<?php
/**
 * 菜洞期数逻辑模块处理
 * User：LLW
 * Date：2020-04-07
 * Time：15：51
 */

namespace app\caidongweb\model;

use think\Db;

class RaiseModel extends BaseModel
{
    protected $table = 'cd_raise';

    /**
     * 众筹首页
     * @return \think\Paginator
     */
    public function getList()
    {
        $where = [];
        $sta = strtotime(input('start',null));
        $end = strtotime(input('end',null));
        if($sta && $end){
            $where['ra_atime'] = array('GT',$sta);
            $where['ra_ltime'] = array('LT',$end);
        }
        input('key') ? $where[input('city')] = input('key') : false;
        $list = $this->where($where)->order('ra_nper desc')->paginate(20,false,['query'=>request()->param()]);
        return $list;
    }

    /**
     * 发布新一期众筹
     * @return array
     */
    public function postAdd()
    {
        //获取相关数据
        $data = input('post.');

        //进行发布前的数据验证
        if(empty($data['ra_nper']) || empty($data['ra_num']) || empty($data['ra_ltime'])){
            return $this->returnData(-1,'必填项不可为空');
        }
        $find = $this->where('ra_nper',$data['ra_nper'])->find();
        if(!empty($find)){
            return $this->returnData(-1,'该期数已经发布过了');
        }
        if($data['ra_state'] == 1){
            $state = $this->where('ra_state',1)->find();
            if(!empty($state)){
                return $this->returnData(-1,'进行中的菜洞只能存在一个');
            }
        }
        if($data['ra_num'] <= 0){
            return $this->returnData(-1,'参与人数不能小于0人');
        }

        //发布数据
        $data['ra_atime'] = time();
        $data['ra_ltime'] = strtotime($data['ra_ltime']);
        $res = $this->insert($data);
        if($res){
            accountLog(2,1,session('sname'),'发布第'.$data['ra_nper'].'期众筹项目');
            return $this->returnData(1,'发布成功');
        }else{
            return $this->returnData(-1,'发布失败');
        }
    }

    /**、
     * 结束排期逻辑
     * @return array
     */
    public function getForce()
    {
        //获取相关数据
        $id = input('id',null);
        $ruident = Db::name('raise_user');
        $reborn_one = bcdiv(tpConfig('reborn_one'),100,4);//1期返回比例
        $reborn_two = bcdiv(tpConfig('reborn_two'),100,4);//2期返回比例
        $reborn_three = bcdiv(tpConfig('reborn_three'),100,4);//3期返回比例
        $reborn_four = bcdiv(tpConfig('reborn_four'),100,4);//4期返回比例
        //获取上期
        $find_one = $this->where('ra_id',$id)->find();
        if(empty($find_one)){
            return $this->returnData(-1,'记录获取失败，请刷新重试');
        }
        $this->where('ra_id',$id)->setField('ra_state',3);
        $this->where(['ra_nper'=>$find_one['ra_nper']-1,'ra_state'=>2])->setField('ra_state',3);
        $find_two = $this->where(['ra_nper'=>$find_one['ra_nper']-1,'ra_state'=>2])->find();//前1期
        $this->where(['ra_nper'=>$find_one['ra_nper']-2,'ra_state'=>2])->setField('ra_state',3);//前2期
        $find_three = $this->where(['ra_nper'=>$find_one['ra_nper']-2,'ra_state'=>2])->find();//前2期
        $this->where(['ra_nper'=>$find_one['ra_nper']-3,'ra_state'=>2])->setField('ra_state',3);//前3期
        $find_four = $this->where(['ra_nper'=>$find_one['ra_nper']-3,'ra_state'=>2])->find();//前3期

        //获取参与人数
        $raise_one = $ruident->where('ru_raid',$find_one['ra_id'])->select();
        $raise_two = $ruident->where('ru_raid',$find_two['ra_id'])->select();
        $raise_three = $ruident->where('ru_raid',$find_three['ra_id'])->select();
        $raise_four = $ruident->where('ru_raid',$find_four['ra_id'])->select();

        //修改数据
        foreach ($raise_one as $v){
           $state_one = $ruident->where('ru_id',$v['ru_id'])->setField('ru_state',2);
           $money_one = bcmul($v['ru_money'],$reborn_one,4);
           accountMoneyLog($v['ru_uid'],$money_one,'爆仓退回',-$v['ru_money']);
        }
        foreach ($raise_two as $val){
            $state_two = $ruident->where('ru_id',$val['ru_id'])->setField('ru_state',2);
            $money_two = bcmul($val['ru_money'],$reborn_two,4);
            accountMoneyLog($val['ru_uid'],$money_two,'爆仓退回',-$val['ru_money'],$val['ru_money']-$money_two);
        }
        foreach ($raise_three as $value){
            $state_three = $ruident->where('ru_id',$value['ru_id'])->setField('ru_state',2);
            $money_three = bcmul($value['ru_money'],$reborn_three,4);
            accountMoneyLog($value['ru_uid'],$money_three,'爆仓退回',-$value['ru_money'],$value['ru_money']-$money_three);
        }
        foreach ($raise_four as $itme){
            $state_four = $ruident->where('ru_id',$itme['ru_id'])->setField('ru_state',2);
            $money_four = bcmul($itme['ru_money'],$reborn_four,4);
            accountMoneyLog($itme['ru_uid'],$money_four,'爆仓退回',-$itme['ru_money'],$itme['ru_money']-$money_four);//退还USDT
        }
        return $this->returnData(1,'处理成功');
    }
}